package com.lzq.rabbitmqdemo.advanced_features.controller;

import com.lzq.rabbitmqdemo.advanced_features.constant.Constants;
import jakarta.annotation.Resource;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/producer")
public class AdvancedProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Resource(name = "confirmRabbitTemplate")
    private RabbitTemplate confirmRabbitTemplate;

    @RequestMapping("/ack")
    public String ack() {
        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE_NAME, "ack",
                "consumer ack test...");
        return "确认成功";
    }

    @RequestMapping("/confirm")
    public String confirm() throws InterruptedException {
        CorrelationData correlationData1 = new CorrelationData("1");
//        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE_NAME,
//                "confirm1", "confirm test...", correlationData1);
        //发送失败
        confirmRabbitTemplate.convertAndSend("confirm_exchange1", "confirm",
                "confirm test...", correlationData1);
        Thread.sleep(2000);
        return "确认成功";
    }

    @RequestMapping("/msgReturn")
    public String msgReturn() {
        CorrelationData correlationData = new CorrelationData("2");
        confirmRabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE_NAME,
                "confirm11", "confirm test...", correlationData);
        return "确认成功";
    }

    @RequestMapping("/retry")
    public String retry(){
        rabbitTemplate.convertAndSend(Constants.RETRY_EXCHANGE_NAME, "", "retry test...");
        return "发送成功!";
    }

    @RequestMapping("/ttl")
    public String ttl(){
        String ttlTime = "10000";
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE_NAME, "", "ttl test...",
                messagePostProcessor -> {
                    messagePostProcessor.getMessageProperties().setExpiration(ttlTime);
                    return messagePostProcessor;
                });
        return "发送成功!";
    }

    @RequestMapping("/dlx")
    public String dlx(){
//        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE_NAME, "normal", "dlx test...");
//        for (int i = 0; i < 20; i++) {
//           rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE_NAME, "normal", "dlx test...");
//        }
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE_NAME, "normal", "dlx test...");
        return "发送成功!";
    }

    @RequestMapping("/delay")
    public String delay() {
        //发送带ttl的消息
        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE_NAME, "normal", "ttl test 10s..."+new Date(),
                messagePostProcessor -> {
                    messagePostProcessor.getMessageProperties().setExpiration("10000");//10s过期
                    return messagePostProcessor;
                });
//        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE_NAME, "normal", "ttl test 20s..."+new Date(),
//                messagePostProcessor -> {
//                    messagePostProcessor.getMessageProperties().setExpiration("20000");//20s过期
//                    return messagePostProcessor;
//                });
        return "发送成功!";
    }
}
